import { Component, OnInit } from '@angular/core';
import { ActivatedRoute } from '@angular/router';
import { NzModalService } from 'ng-zorro-antd';

declare var form: any;

@Component({
  templateUrl: './portal.message.board.info.component.html'
})
export class PortalMessageBoardInfoComponent implements OnInit {

  // 当前数据
  fs: any;
  sideDic: any = null;
  modeDic: any = null;

  constructor(
    public route: ActivatedRoute,
    private modalService: NzModalService) {
    // 注册表单
    this.fs = new form.FormSimpleService('cms_messageboard', {
      orderObject: { id: 0 },
      keyField: 'id',
      randomFields: ['id']
    });

    this.sideDic = new form.DictionaryService('cms_site');
    this.sideDic.loadAll();
    this.modeDic = new form.DictionaryService('cms_messageclass');
    this.modeDic.loadAll();
  }

  ngOnInit() {
    this.initData();

    // 浏览器关闭监听
    window.onbeforeunload = () => {
      // tslint:disable-next-line: deprecation
      const e: any = window.event;
      e.returnValue = ('请确认已保存，是否离开？');
      return e;
    };
  }

  initData() {
    let id = '-1';
    this.route.queryParams.subscribe((res) => {
      if (res.id) {
        id = res.id;
      }
    });
    this.fs.editRowByKey(id);
  }

  save() {
    this.fs.save(this.fs.er, (data) => {
      this.modalService.success({
        nzTitle: '系统提示',
        nzContent: '保存成功！'
      });
      this.fs.er = data;
    }, (error) => {
      this.modalService.error({
        nzTitle: '系统提示',
        nzContent: '保存失败！'
      });
    });
  }

  // 通过
  pass() {
    this.fs.er.state = '2';
    this.save();
  }

  // 不通过
  noPass() {
    this.fs.er.state = '3';
    this.save();
  }

  close() {
    window.close();
  }

  // 站点下拉选择后
  treeClick(e) {
    this.fs.er.type = null;
    this.modeDic.filter((item) => {
      if (item.other.siteId === e) {
        return true;
      } else {
        return false;
      }
    });
  }
}
